﻿/// <reference path="DragablePanel.js" />
/// <reference path="../Utility/Hashtable.js" />
//窗体状态 (最大化,最小化,普通)
IM.Control.WindowState =
{
    Max: 1,
    Min: 2,
    Normal: 3
};

//窗体管理器
IM.Control.WindowManager = function() {
    IM.Utility.ArrayList.apply(this, arguments);

    this.MiniWindows = new IM.Utility.ArrayList(); //最小化状态窗体集合
    //将某窗体前置
    this.SetTopZIndex = function(win) {
        if (typeof win == "undefined")
            return;
        var maxZIndex = Number(win.GetZIndex());
        for (var i = 0; i < this.Items.length; i++) {
            this.Items[i].SetActiveStyle(false); //将所有窗体设置为非激活状态
            this.Items[i].IsActive = false;
            this.Items[i].Render.blur();
            if (Number(this.Items[i].GetZIndex()) > Number(win.GetZIndex())) {
                if (Number(this.Items[i].GetZIndex()) > maxZIndex)
                    maxZIndex = Number(this.Items[i].GetZIndex());
                this.Items[i].SetZIndex(Number(this.Items[i].GetZIndex()) - 1);
            }
        }
        win.SetZIndex(maxZIndex);
        win.SetActiveStyle(true);
        win.Render.focus();
        win.IsActive = true;
    };

    //获取最大zindex值
    this.GetMaxZIndex = function() {
        var maxZIndex = 1000;
        for (var i = 0; i < this.Items.length; i++) {
            if (Number(this.Items[i].GetZIndex()) > maxZIndex)
                maxZIndex = Number(this.Items[i].GetZIndex());
        }
        return maxZIndex;
    };

    //重新排列所有最小化窗口
    this.ResetMiniWindow = function() {
        if (this.MiniWindows.Count() == 0)
            return;

        //获取这些窗体容器的宽度

        var containerWidth;

        if (typeof this.MiniWindows.Find(0).Container.IsMdiContainer != "undefined" && this.MiniWindows.Find(0).Container.IsMdiContainer) {
            //如果当前窗体的容器是多文档窗体,最大化时，大小与多文档窗体的内容区相同
            containerWidth = this.MiniWindows.Find(0).Container.Content.width();
        }
        else {
            containerWidth = this.MiniWindows.Find(0).Container.width();
        }

        //如果最小化窗体宽度之和超过了容器的宽度，则将小窗体宽度减小
        var miniWindowWidth = this.MiniWindows.Find(0).GetWidth() + 4 < (containerWidth / this.MiniWindows.Count()) ? this.MiniWindows.Find(0).GetWidth() : containerWidth / this.MiniWindows.Count() - 4;

        for (var i = 0; i < this.MiniWindows.Count(); i++) {
            this.MiniWindows.Find(i).SetWidth(miniWindowWidth);
            this.MiniWindows.Find(i).SetLeft(4 + (4 + miniWindowWidth) * i);
        }
    };

    this.AddWindow = function() {
        var win;

        this.Add(arguments[0]);
        win = arguments[0];

        win.SetZIndex(Number(this.GetMaxZIndex()) + 1);

        this.SetTopZIndex(win);
    };

    //删除窗体
    this.RemoveWindow = function(window) {
        var index = -1;
        for (var i = 0; i < this.Items.length; i++) {
            if (this.Items[i] == window) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            for (var i = index; i < this.Items.length - 1; i++) {
                this.Items[i] = this.Items[i + 1];
            }
            this.Items.pop();
        }
    };

    this.WindowManagerInit = function() {
        //var oldAdd = this.Add;
    };

    this.WindowManagerInit();
};

IM.Control.DefaultWindowManager = new IM.Control.WindowManager();

//窗体类
IM.Control.Window = function(conf) {
    IM.Control.DragablePanel.apply(this, arguments);
    this.Title; //顶部标题文字区
    this.IsActive = true;
    this.Text;
    this.Header; //顶部
    this.Content; //内容区
    this.HeaderCloseButton; //顶端关闭按钮
    this.HeaderMaxButton; //顶端最大化按钮
    this.HeaderMinButton; //顶端最小化按钮
    this.HeaderNormalButton; //恢复普通状态按钮
    this.ShowHeader = true; //是否显示顶部
    this.Closable = true; //是否可以关闭
    this.MinSizable = true; //是否可以最小化
    this.MaxSizable = true; //是否可以最大化
    this.WindowState; //当前窗口状态,默认普通
    this.Manager; //当前窗体的窗体管理器
    this.OnMaxSized; //最大化事件
    this.OnNormalSized; //窗口恢复事件
    this.OnMinSized; //窗口最小化事件

    var normalSize; //非最大最小化时的大小
    var normalPosition; //非最大最小化时的座标
    var originalState; //原来的状态
    this.ControlArea;
    this.IsMdiContainer; //待实现多文档

    //转到最大化状态
    this.ChangeToMax = function() {
        if (this.WindowState == IM.Control.WindowState.Normal)//如果当前状态是普通，记录当前窗口信息
            this.SaveNormalState();

        originalState = this.WindowState; //存储当前的窗体状态

        if (typeof this.Container.IsMdiContainer != "undefined" && this.Container.IsMdiContainer) {
            //如果当前窗体的容器是多文档窗体,最大化时，大小与多文档窗体的内容区相同
            this.SetTop(this.Container.Content.offset().top);
            this.SetLeft(this.Container.Content.offset().left);
            this.SetWidth(this.Container.Content.width());
            this.SetHeight(this.Container.Content.height());

        }
        else {
            this.SetTop(this.Container.offset().top);
            this.SetLeft(this.Container.offset().left);
            this.SetWidth(this.Container.width());
            this.SetHeight(this.Container.height());
        }

        this.SetDrag(false);
        this.HeaderMaxButton.hide();
        this.HeaderNormalButton.show();
        this.HeaderCloseButton.show();
        this.HeaderMinButton.show();

        this.Manager.MiniWindows.Remove(this); //将自己放到最小化窗口集合中
        this.Manager.ResetMiniWindow(); //根据容器大小重新调整所有最小化窗口

        this.Title.width(this.Header.width() - this.ControlArea.width() - 10);
        this.Content.height(this.Render.height() - this.Header.height());
        this.WindowState = IM.Control.WindowState.Max;

        if (this.OnMaxSized)
            this.OnMaxSized();
    };

    //转到最小化状态
    this.ChangeToMin = function() {
        if (this.WindowState == IM.Control.WindowState.Normal)//如果当前状态是普通，记录当前窗口信息
            this.SaveNormalState();

        originalState = this.WindowState; //存储当前的窗体状态

        if (typeof this.Container.IsMdiContainer != "undefined" && this.Container.IsMdiContainer) {
            this.SetTop(this.Container.Content.offset().top + this.Container.Content.height() - this.Header.height() - 10);
            this.SetLeft(this.Container.Content.offset().left);
            this.SetWidth(80);
            this.SetHeight(this.Header.height() + 5);
        }
        else {
            this.SetTop(this.Container.height() - this.Header.height() - 10);
            this.SetLeft(0);
            this.SetWidth(80);
            this.SetHeight(this.Header.height() + 5);
        }

        this.SetDrag(false);
        this.HeaderMaxButton.hide();
        this.HeaderNormalButton.hide();
        this.HeaderCloseButton.hide();
        this.HeaderMinButton.hide();
        this.Manager.MiniWindows.Add(this); //将自己放到最小化窗口集合中
        this.Manager.ResetMiniWindow(); //根据容器大小重新调整所有最小化窗口

        this.WindowState = IM.Control.WindowState.Min;

        if (this.OnMinSized)
            this.OnMinSized();
    };

    //转换到普通状态
    this.ChangeToNormal = function() {

        originalState = this.WindowState; //存储当前的窗体状态

        if (typeof this.Container.IsMdiContainer != "undefined" && this.Container.IsMdiContainer) {
            if (typeof normalPosition != "undefined" && typeof normalSize != "undefined") {
                //恢复原来的窗体普通状态
                this.SetTop(normalPosition.Y);
                this.SetLeft(normalPosition.X);
                this.SetWidth(normalSize.Width);
                this.SetHeight(normalSize.Height);
            }

        }
        else {


            if (typeof normalPosition != "undefined" && typeof normalSize != "undefined") {
                //恢复原来的窗体普通状态

                this.SetTop(normalPosition.Y);
                this.SetLeft(normalPosition.X);
                this.SetWidth(normalSize.Width);
                this.SetHeight(normalSize.Height);
            }
        }
        this.SetDrag(true);

        this.HeaderCloseButton.show();
        this.HeaderMinButton.show();
        this.HeaderMaxButton.show();
        this.HeaderNormalButton.hide();

        this.Manager.MiniWindows.Remove(this); //将自己放到最小化窗口集合中
        this.Manager.ResetMiniWindow(); //根据容器大小重新调整所有最小化窗口

        this.Title.width(this.Header.width() - this.ControlArea.width() - 10);
        this.Content.height(this.Render.height() - this.Header.height());
        this.WindowState = IM.Control.WindowState.Normal;
        if (this.OnNormalSized)
            this.OnNormalSized();
    };

    //保存当前的普通状态
    this.SaveNormalState = function() {
        normalSize = { Width: this.Render.width(), Height: this.Render.height() };
        normalPosition = { X: this.Render.offset().left, Y: this.Render.offset().top };
    };

    this.SetActiveStyle = function(isActive) {
        if (typeof isActive == "boolean") {
            if (isActive) {
                this.Header.removeClass("IM_WindowUnActiveHeader");
                this.Header.addClass("IM_WindowActiveHeader");
            } else {
                this.Header.removeClass("IM_WindowActiveHeader");
                this.Header.addClass("IM_WindowUnActiveHeader");
            }
        }
    };

    //从最小化来恢复
    this.RestoreFromMini = function() {
        if (this.WindowState == IM.Control.WindowState.Min) {
            if (originalState == IM.Control.WindowState.Max) {
                this.ChangeToMax();
            }
            else {
                this.ChangeToNormal();
            }
        }
    };

    //关闭窗体
    this.Close = function() {
        if (typeof this.Render == "object") {
            this.Render.hide();
            this.Manager.RemoveWindow(this);
        }
    };

    this.Show = function() {
        if (typeof this.Render == "object") {
            if (!this.GetVisible()) {
                this.Render.show();
                this.Manager.AddWindow(this);
            }
            else {
                this.Manager.SetTopZIndex(this);
            }
        }
    };

    this.GetVisible = function() {
        return this.Render.is(":visible");
    };

    this.SetTitle = function(title) {
        this.Title.attr({ "title": title.replace("&nbsp;", "") });
        this.Title.html(title);
    };

    this.GetTitle = function() {
        return this.Title.html();
    };

    //窗体初始化
    this.WindowInit = function() {
        if (typeof this.Render == "undefined")
            return;

        if (typeof this.WindowState == "undefined" || this.WindowState == null)//如果初始化时没有没有窗体状态设置为普通状态
            this.WindowState = IM.Control.WindowState.Normal;


        //初始化标题和内容区
        this.Header = $("<div unselectable='on'></div>");
        this.Render.append(this.Header);
        this.Header.addClass("IM_WindowActiveHeader");

        //按钮在的层
        this.ControlArea = $("<div class='IM_WindowControlBox' unselectable='on' style='width:80px;clear:right;float:left;'></div>");
        this.ControlArea.width(80);
        this.Header.append(this.ControlArea);

        //初始化关闭按钮
        this.HeaderCloseButton = $("<div unselectable='on'></div>");
        this.HeaderCloseButton.addClass("IM_Window_HeaderButton IM_Window_Active_CloseButton");
        this.ControlArea.append(this.HeaderCloseButton);
        this.HeaderCloseButton.bind("click", (function(obj) { return function(e) { obj.Close(); e.originalEvent.cancelBubble = true; }; })(this));

        //初始化最大化按钮
        this.HeaderMaxButton = $("<div unselectable='on'></div>");
        this.HeaderMaxButton.addClass("IM_Window_HeaderButton IM_Window_Active_MaxButton");
        this.ControlArea.append(this.HeaderMaxButton);
        this.HeaderMaxButton.bind("click", (function(obj) { return function() { obj.ChangeToMax(); }; })(this));

        //初始化普通状态按钮
        this.HeaderNormalButton = $("<div unselectable='on'></div>");
        this.HeaderNormalButton.addClass("IM_Window_HeaderButton IM_Window_Active_NormalButton");
        this.ControlArea.append(this.HeaderNormalButton);
        this.HeaderNormalButton.bind("click", (function(obj) { return function() { obj.ChangeToNormal(); }; })(this));

        //初始化最小化按钮
        this.HeaderMinButton = $("<div unselectable='on'></div>");
        this.HeaderMinButton.addClass("IM_Window_HeaderButton IM_Window_Active_MinButton");
        this.ControlArea.append(this.HeaderMinButton);
        this.HeaderMinButton.bind("click", (function(obj) { return function() { obj.ChangeToMin(); }; })(this));

        //初始化标题区的按钮(最大化，最小化，恢复普通，关闭)
        this.Title = $("<div class='IM_WindowTitle' unselectable='on' style='float:left;clear: left;overflow: hidden;padding-left: 10px;line-height: 20px;'></div>");
        if (typeof this.Text == "undefined")
            this.Text = "&nbsp;";

        this.SetTitle(this.Text);
        this.Header.prepend(this.Title);
        this.Title.width(this.Header.width() - this.ControlArea.width() - 10);

        //初始化内容区
        this.Content = $("<div></div>");
        this.Content.css("overflow", "hidden");
        this.Render.append(this.Content);
        this.Content.height(this.Render.height() - this.Header.height());

        //分成两种情况，如果当前窗体的容器是多文档窗体，当前窗体最大化最小化时大小及座标以当前窗体的Content区为准，如果不是以Container为准
        //容器为多文档窗体
        if (typeof this.Container.IsMdiContainer != "undefined" && this.Container.IsMdiContainer) {
            this.SetTop((this.Container.Content.height() - this.GetHeight()) / 2 - this.Container.Content.offset().top); //设置初始座标
            this.SetLeft((this.Container.Content.width() - this.GetWidth()) / 2 - this.Container.Content.offset().left);
            this.SetPosition("relative");
            this.Container.Content.append(this.Render);
            //alert(aaaa);
            //this.Content.height(this.Container.Content.height() - this.Header.height());

        } //容器为非多文档窗体
        else {
            this.SetTop((this.Container.height() - this.GetHeight()) / 2);
            this.SetLeft((this.Container.width() - this.GetWidth()) / 2);

            //this.Content.height(this.Container.height() - this.Header.height());
        }

        this.Render.bind("mousedown", (function(obj) { return function() { if (!obj.IsActive) obj.Manager.SetTopZIndex(obj); }; })(this)); //为窗体添加鼠标按下事件，用来激活当前窗体
        //鼠标弹起时恢复窗体
        this.Header.bind("mouseup",
        (function(obj) {
            return function() {
                if (IM.Mouse.Location.X < obj.HeaderMinButton.offset().left || IM.Mouse.Location.X > obj.HeaderMinButton.offset().left + obj.HeaderMinButton.width() ||
            IM.Mouse.Location.Y < obj.HeaderMinButton.offset().top || IM.Mouse.Location.Y > obj.HeaderMinButton.offset().top + obj.HeaderMinButton.height())
                    obj.RestoreFromMini();
            };
        })(this));

        this.Header.bind("dblclick", (function(obj) {
            return function() {
                if (obj.WindowState == IM.Control.WindowState.Max) {
                    obj.ChangeToNormal();
                }
                else if (obj.WindowState == IM.Control.WindowState.Normal) {
                    obj.ChangeToMax();
                }
            };
        })(this));

        this.DragHandler = this.Header; //设置窗体拖曳区域为标题区
        this.DragablePanelInit(); //初始化拖曳

        this.SaveNormalState();
        originalState = IM.Control.WindowState.Normal;

        //将当前窗体没有管理器，则使用默认管理器
        if (typeof this.Manager == "undefined") {
            this.Manager = IM.Control.DefaultWindowManager;
        }
        //将当前窗体加入管理器
        this.Manager.AddWindow(this);

        switch (this.WindowState) {
            case IM.Control.WindowState.Max:
                this.ChangeToMax(); break;
            case IM.Control.WindowState.Min:
                this.ChangeToMin(); break;
            case IM.Control.WindowState.Normal:
                this.ChangeToNormal(); break;
        }

        this.Render.focus();
    };

    this.WindowInit();
};



